package jdbc.jdbc_Text;

import java.sql.*;
import java.util.Scanner;

public class JDBCDemo_Text2 {
    /**
     * 连接mysql实现对用户输入的账号密码进行判定
     * @param args
     * @throws SQLException
     */
    public static void main(String[] args) throws SQLException {

        //接收用户输入的账号密码
        System.out.println("请你输入账号");
        String nmber=new Scanner(System.in).next();
        System.out.println("请你输入密码");
        String mima=new Scanner(System.in).next();
        //sql注入:
        // mima="'or'r'='r";
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/学生库", "root", "mysql");
        //定义sql语句
        //String sql="insert pwd value('zhangsan','123')";
        //查询账号密码是否在数据库中
        String sql="select * from pwd where accountnumber='"+nmber+"' and password='"+mima+"'";
        //定义sql对象
        Statement statement = connection.createStatement();
        //执行语句
        //int i = statement.executeUpdate(sql);
        ResultSet resultSet = statement.executeQuery(sql);
        if (resultSet.next()){
            System.out.println("登录成功");
        }else{
            System.out.println("登录失败");

        }
        //关流
        resultSet.close();
        statement.close();
        connection.close();
    }
}
